import json
import re

import requests


def get_token_from_js():
    url = "http://zhaopin.baidu.com/quanzhi?query=python&city=%E5%8C%97%E4%BA%AC"

    # 整理请求头
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
        "cookie": "BAIDUID=C9F168C021436C5FB903DED17AFC22D3:FG=1; BIDUPSID=C9F168C021436C5FB903DED17AFC22D3; PSTM=1605177720; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDUSS=1XbWE5VFhmYmNLUURRRDNFREZJdUxyeTJVWVlZOUViZ3p5fjVNdGJDTGdOZFpmRUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOCorl~gqK5fam; yjs_js_security_passport=66fc8b40189705f88efd5cca35b89b7fe4b7996f_1605517924_js; Hm_lvt_da3258e243c3132f66f0f3c247b48473=1605171135,1605226299,1605519861; Hm_lpvt_da3258e243c3132f66f0f3c247b48473=1605519861; H_PS_PSSID=32819_1452_33041_33061_31253_32971_33098_33100_32962_32938; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=5"
    }

    # 获取请求
    r = requests.get(url, headers=headers)

    # 提取nekot
    html = r.content.decode()

    # print(html)

    token = re.search(r'data\["nekot"\] = "(.*?)";', html).group(1)
    print(token)
    return token


def get_token():
    token = get_token_from_js()
    return token[::-1]  # 逆序字符串


def get_position(token):
    url = "http://zhaopin.baidu.com/api/qzasync?query=python&city=北京&is_adq=1&pcmod=1&token=%s&pn=20&rn=20" % token

    # 整理请求头
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
        "cookie": "BAIDUID=C9F168C021436C5FB903DED17AFC22D3:FG=1; BIDUPSID=C9F168C021436C5FB903DED17AFC22D3; PSTM=1605177720; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDUSS=1XbWE5VFhmYmNLUURRRDNFREZJdUxyeTJVWVlZOUViZ3p5fjVNdGJDTGdOZFpmRUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOCorl~gqK5fam; yjs_js_security_passport=66fc8b40189705f88efd5cca35b89b7fe4b7996f_1605517924_js; Hm_lvt_da3258e243c3132f66f0f3c247b48473=1605171135,1605226299,1605519861; Hm_lpvt_da3258e243c3132f66f0f3c247b48473=1605519861; H_PS_PSSID=32819_1452_33041_33061_31253_32971_33098_33100_32962_32938; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=5"
    }

    # 获取请求
    r = requests.get(url, headers=headers)

    # 返回获取到的数据
    return json.loads(r.content.decode())


def get_info(position_dict):
    position_list = position_dict.get("data").get("disp_data")
    for position in position_list:
        print("--------------------------")
        print("岗位名：", position.get("@name"))
        print("薪资：", position.get("salary"))
        print("公司：", position.get("company"))


def main():
    # 1. 获取token
    token = get_token()
    print("提取的token为:", token)

    # 2. 发送请求获取职位信息
    position_dict = get_position(token)
    # print("获取的岗位信息是：", position_dict)

    # 3. 提取数据
    get_info(position_dict)


if __name__ == '__main__':
    main()
